/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.dojotec.biblioteca.jsf.application;

import com.dojotec.biblioteca.jsf.dao.ReservaDAO;
import com.dojotec.biblioteca.jsf.domain.Reserva;
import com.dojotec.biblioteca.jsf.domain.ReservaRepository;
import java.util.Calendar;
import java.util.List;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author igocoelho
 */
public class ReservaBean {

    private DataModel reservas = null;
    private List<Reserva> listaReservas;
    private ReservaRepository reserva = new ReservaDAO();
    private Reserva reservaPojo = new Reserva();
    
    public ReservaBean() {
        
    }
    
    public String gotoNovaReserva() {
        getReservaPojo().setDataReserva(new java.sql.Date(Calendar.getInstance().getTimeInMillis()));
        return "novaReserva";
    }
    
    public String gotoAlteraReserva() {
        setReservaPojo((Reserva)reservas.getRowData());
        return "alteraReserva";
    }
    
    public String gotoListaReservas() {
        return "listaReservas";
    }

    public DataModel getReservas() {
        return reservas;
    }

    public void setReservas(DataModel reservas) {
        this.reservas = reservas;
    }

    public ReservaRepository getReserva() {
        return reserva;
    }

    public void setReserva(ReservaRepository reserva) {
        this.reserva = reserva;
    }

    public Reserva getReservaPojo() {
        return reservaPojo;
    }

    public void setReservaPojo(Reserva reservaPojo) {
        this.reservaPojo = reservaPojo;
    }
    
    public DataModel getTodasReservas() {
        setReservas(new ListDataModel(getReserva().buscarTodos()));
        return getReservas();
    }
    
    public String salvar() {
        try {
            reserva.salvar(getReservaPojo());
            return gotoListaReservas();
        } catch (Exception e) {
            return "failure";
        }
    }
    
    public String atualizar() {
        try {
            reserva.atualizar(getReservaPojo());
            return gotoListaReservas();
        } catch (Exception e) {
            return "failure";
        }    
    }
    
    public String excluir() {
        try {
            setReservaPojo((Reserva)reservas.getRowData());
            reserva.excluir(getReservaPojo());
            return gotoListaReservas();
        } catch (Exception e) {
            return "failure";
        }
    }

    public List<Reserva> getListaReservas() {
        if (listaReservas==null) {
            listaReservas = getReserva().buscarTodos();
        }
        return listaReservas;
    }

    public void setListaReservas(List<Reserva> listaReservas) {
        this.listaReservas = listaReservas;
    }
    
}
